﻿using UnityEngine;
using System.Collections;

public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
	protected static T instance;

	public static T Instance
	{
		get
		{
			if (instance == null)
			{
				instance = GameObject.FindObjectOfType<T>();
				
				if (instance == null)
				{
					Debug.LogError("An instance of " + typeof(T) + " isn't available.");
				}
			}
			
			return instance;
		}
	}
	
	private void OnDestroy()
	{
		instance = null;
	}

	public static bool Exists()
	{
		return instance != null;
	}
}
